﻿@model Admin.CYT369.Com.DAL.Domains.SysUser
@{
    Layout = "_IndexLayout";
    var avatarImgCtlName = PageViewModel.ControlID(this, "avatar_img");
    var changePwdDlgName = PageViewModel.PageEditDialog(this, "change", "password");
    var onChangePwdFunName = PageViewModel.PageFunctionName(this, "on_change_pwd", "user");
    var saveChangePwdFunName = PageViewModel.PageFunctionName(this, "save_change_pwd", "user");

    var changeAvatarDlgName = PageViewModel.PageEditDialog(this, "change", "avatar");
    var onChangeAvatarFunName = PageViewModel.PageFunctionName(this, "on_change_avatar", "user");
    var saveChangeAvatarFunName = PageViewModel.PageFunctionName(this, "save_change_avatar", "user");
}

@section ContentPanel{
    <div style="height:100%;margin:50px">
        <style type="text/css">
            ._user_profile_header_style_name {
                width: 80px;
                height: 80px;
                border-radius: 40px;
                line-height: 80px;
                margin-bottom: 10px;
                font-size: 20px;
                text-align: center;
                background-color: #83a9ff;
                color: white;
            }

            ._user_profile_header_style_img {
                width: 80px;
                height: 80px;
                border-radius: 50px;
                margin-bottom: 10px;
                border: solid 1px #eee;
                padding: 3px;
            }

            ._change_user_profile_header_btn {
                width: 80px;
                height: 30px;
                text-align: center;
                line-height: 30px;
                background-color: #f5f5f5;
                cursor: pointer;
            }
        </style>
        @if (Model != null)
        {
            <script type="text/javascript">
            function @onChangePwdFunName () {
                var dlg = $dlg('@changePwdDlgName');
                dlg.dialog({
                    title: '修改密码', width: 450, height: 250,
                    closed: false, cache: false, modal: true, maximizable: false, resizable: true,
                    href: '~/Account/_ChangePasswordDlg',
                    onResize: function () {
                        $(this).dialog('center');
                    },
                    buttons: [
                        {
                            text: "确 定",
                            handler: function () { @saveChangePwdFunName (); },
                            iconCls: "icon-save"
                        }, {
                            text: "取 消",
                            handler: function () {
                                $("#@changePwdDlgName").dialog("close");
                            },
                            iconCls: "icon-cancel"
                        }
                    ]
                });
            }

            function @saveChangePwdFunName () {
                var form = $("#@changePwdDlgName").find("form");
                if (form.form('enableValidation').form('validate')) {
                    var old = form.find('#OldPassword').textbox('getValue');
                    var new1 = form.find('#New1Password').textbox('getValue');
                    var new2 = form.find('#New2Password').textbox('getValue');

                    if (new1 === new2) {
                        if (old !== new1) {
                            if (new1.length >= 6) {
                                $.ajax({
                                    url: "~/Account/ChangeMyPassword",
                                    type: "POST",
                                    data: {
                                        OldPassword: old,
                                        New1Password: new1,
                                        New2Password: new2
                                    },
                                    success: function (re) {
                                        $.messager.alert('成功', '成功修改密码。', 'info', function () {
                                            $("#@changePwdDlgName").dialog('close');
                                        });
                                    },
                                    error: function (e) {
                                        ShowAjaxError(e);
                                    }
                                });
                            } else {
                                $.messager.alert('提示', '新密码长度要求至少6位。', 'warning');
                            }
                        } else {
                            $.messager.alert('提示', '新密码不能与原密码一致。', 'warning');
                        }
                    } else {
                        $.messager.alert('提示', '两次输入的新密码不一致，请再次确认输入的新密码一致。', 'warning');
                    }
                }
            }
            </script>

            <script type="text/javascript">
            function @onChangeAvatarFunName () {
                var dlg = $dlg('@changeAvatarDlgName');
                dlg.dialog({
                    title: '修改头像', width: 450, height: 250,
                    closed: false, cache: false, modal: true, maximizable: false, resizable: true,
                    href: '~/Account/_ChangeUserAvatarDlg',
                    onResize: function () {
                        $(this).dialog('center');
                    },
                    buttons: [
                        {
                            text: "确 定",
                            handler: function () { @saveChangeAvatarFunName (); },
                            iconCls: "icon-save"
                        }, {
                            text: "取 消",
                            handler: function () {
                                $("#@changeAvatarDlgName").dialog("close");
                            },
                            iconCls: "icon-cancel"
                        }
                    ]
                });
            }

            function @saveChangeAvatarFunName () {
                var dlg = $('#@changeAvatarDlgName');
                dlg.find("form").form("submit", {
                    url: $.Content('~/Account/SaveUserAvatar'),
                    dataType: "json",
                    success: function (str) {
                        try {
                            var e = JSON.parse(str);
                            if (e) {
                                if (e.Result) {
                                    var ctl = $("#@avatarImgCtlName");
;                                    if (ctl[0].tagName == "img") {
                                        ctl.attr("src", e.Avatar);
                                    } else {
                                        $("<img id='@avatarImgCtlName' src='" + e.Avatar + "' class='_user_profile_header_style_img'/>").insertAfter(ctl);
                                        ctl.remove();
                                    }


                                    var ha = $(".TopPanel .RPanel .PersonalInfo .icon");
                                    if (ha.length > 0) {
                                        ha.attr("src", e.Avatar);
                                    } else {
                                        var va = $(".TopPanel .RPanel .PersonalInfo .n_icon");
                                        if (va.length > 0) {
                                            $("<img src='" + e.Avatar + "' class='icon'/>").insertAfter(va);
                                            va.remove();
                                        }
                                    }

                                    $("#@changeAvatarDlgName").dialog("close");
                                } else {
                                    $.messager.alert('失败', "更新失败", 'error');
                                }
                            }
                        } catch (e) {
                        }
                    },
                    error: function (e) {
                        ShowAjaxError(e);
                    }
                });
            }
            </script>

            <table class="form_table" style="line-height:30px;color:#2b2828;">
                <tr>
                    <td colspan="4"><b style="color:black">基本信息</b></td>
                </tr>
                <tr>
                    <td width="120" align="right">工号：</td>
                    <td colspan="2">@Model.Code</td>
                    <td rowspan="3" style="padding-left: 30px">
                        @if (!string.IsNullOrEmpty(Model.Avatar))
                        {
                            <img id="@avatarImgCtlName" src="@Model.Avatar" class="_user_profile_header_style_img" />
                        }
                        else if (!string.IsNullOrEmpty(Model.HeaderUrl))
                        {
                            <img id="@avatarImgCtlName" src="@Model.HeaderUrl" class="_user_profile_header_style_img" />
                        }
                        else
                        {
                            var name = Model.FullName;
                            if (!string.IsNullOrEmpty(name))
                            {
                                name = Model.FullName.Substring(0, 1);
                            }
                            <div class="_user_profile_header_style_name">@name</div>
                        }
                        <div onclick="@onChangeAvatarFunName ()" class="_change_user_profile_header_btn">修改头像</div>
                    </td>
                </tr>
                <tr>
                    <td align="right">姓名：</td>
                    <td colspan="2">@Model.FullName</td>
                </tr>
                <tr>
                    <td align="right">性别：</td>
                    <td colspan="2">@Model.Gender</td>
                </tr>
                <tr>
                    <td align="right">职称：</td>
                    <td colspan="3">@Model.Title</td>
                </tr>
                <tr>
                    <td align="right">电话：</td>
                    <td colspan="3">@Model.PhoneNumber</td>
                </tr>
                <tr>
                    <td align="right">地址：</td>
                    <td colspan="3">@Model.Address</td>
                </tr>
                <tr>
                    <td align="right">邮件：</td>
                    <td colspan="3">@Model.Email</td>
                </tr>
                <tr>
                    <td colspan="4"><b style="color:black">账户信息</b></td>
                </tr>
                <tr>
                    <td align="right">登录账户：</td>
                    <td>@Model.UserName</td>
                </tr>
                <tr>
                    <td align="right">登录密码：</td>
                    <td width="300">**********</td>
                    <td width="300" colspan="2"><a style="text-decoration:none" href="javascript:@onChangePwdFunName ()">修改</a></td>
                </tr>
                <tr>
                    <td align="right">绑定微信：</td>
                    <td>@(string.IsNullOrEmpty(Model.OpenID) ? "" : "是")</td>
                    <td width="300" colspan="2"><a style="text-decoration:none" href="javascript:void()">绑定</a></td>
                </tr>
            </table>
        }
    </div>
}
